//
//  AIMineYouHuiQuanController.m
//  ZJCLoveBee
//
//  Created by 朱建超 on 16/12/23.
//  Copyright © 2016年 itHeiMa. All rights reserved.
//

#import "AIMineYouHuiQuanController.h"
#import <AFNetworking.h>
#import "LEEHTTPManager.h"
#import "Masonry.h"
#import "YYModel.h"
#import "AIMineYouHuiQuanModel.h"
#import "AIMineYoiuHuiQuanCell.h"
#import "AIMineRuleController.h"

static NSString *youXiaoCellID = @"youXiaoCellID";
static NSString *guoQiCellID = @"guoQiCellID";

@interface AIMineYouHuiQuanController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)NSArray <AIMineYouHuiQuanModel *> *dataArray;
@property(nonatomic,weak) UITableView *mineTableView;

@end

@implementation AIMineYouHuiQuanController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"优惠券";
    
    UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithTitle:@"使用规则" style:UIBarButtonItemStylePlain target:self action:@selector(goRuleController)];
    
    self.navigationItem.rightBarButtonItem = barButton;
    
    NSLog(@"%@",self.navigationItem);
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self getData];
    
    [self setupUI];
    
}

-(void)goRuleController{
    
    AIMineRuleController *ruleController = [[AIMineRuleController alloc] init];
    
    [self.navigationController pushViewController:ruleController animated:YES];
    
}

-(void)setupUI{

    UITextField *textField = [[UITextField alloc] init];
    
    [self.view addSubview:textField];
    
    textField.placeholder = @"请输入优惠券号码";
    textField.layer.cornerRadius = 5;
    textField.layer.masksToBounds = YES;
    
    [textField mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.view).offset(74);
        make.left.mas_equalTo(self.view).offset(20);
        make.width.mas_equalTo(260);
        make.height.mas_equalTo(30);
        
    }];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [self.view addSubview:button];
    
    button.backgroundColor = [UIColor colorWithRed:249.0 / 255.0 green:205.0 / 255.0 blue:40.0 / 255.0 alpha:1];
    [button setTitle:@"绑定" forState:UIControlStateNormal];
    button.titleLabel.textAlignment = NSTextAlignmentCenter;
    button.layer.cornerRadius = 5;
    button.layer.masksToBounds = YES;
    
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(textField.mas_top);
        make.centerY.mas_equalTo(textField.mas_centerY);
        make.left.mas_equalTo(textField.mas_right).offset(15);
        make.height.mas_equalTo(textField.mas_height);
        make.right.mas_equalTo(self.view.mas_right).offset(-20);
        
    }];
    
    UIView *lineView = [[UIView alloc] init];
    
    [self.view addSubview:lineView];
    
    lineView.backgroundColor = [UIColor lightGrayColor];
    
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.right.mas_equalTo(self.view);
        make.height.mas_equalTo(0.5);
        make.top.mas_equalTo(textField.mas_bottom).offset(10);
        
    }];
    
    UITableView *tableView = [[UITableView alloc] init];
    
    [self.view addSubview:tableView];
    _mineTableView = tableView;

    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.rowHeight = 121;
    
    [tableView registerNib:[UINib nibWithNibName:@"AIMineYoiuHuiQuanCell" bundle:nil] forCellReuseIdentifier:youXiaoCellID];
    [tableView registerNib:[UINib nibWithNibName:@"AIMineGrayYouHuiQuanCell" bundle:nil] forCellReuseIdentifier:guoQiCellID];
    
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.bottom.right.mas_equalTo(self.view);
        make.top.mas_equalTo(lineView.mas_bottom);
        
    }];
    

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return self.dataArray.count;

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    AIMineYouHuiQuanModel *youHuiQuanModel = self.dataArray[indexPath.row];
    
    NSString *cellID;
    
    if (youHuiQuanModel.status == 0) {
        
        cellID = youXiaoCellID;
        
    }else{
    
        cellID = guoQiCellID;
    
    }
    
    AIMineYoiuHuiQuanCell *youHuiQuanCell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
    
    youHuiQuanCell.youHuiQuanModel = youHuiQuanModel;
    
    return youHuiQuanCell;

}

-(void)getData{

    NSString *urlString = @"http://iosapi.itcast.cn/loveBeen/MyCoupon.json.php";
    
    NSDictionary *parameters = @{@"call":@"9"};
    
    [[LEEHTTPManager share] request:POST UrlString:urlString parameters:parameters finshed:^(id responseObject, NSError *error) {
        
        if (error != nil) {
            
            NSLog(@"请求失败");
            
        }
        
        NSArray <AIMineYouHuiQuanModel *> *dataArray = responseObject[@"data"];
        
        NSMutableArray *dataMarray = [NSMutableArray array];
            
        for (NSDictionary *dataList in dataArray) {
            
            AIMineYouHuiQuanModel *youHuiQuanModel = [[AIMineYouHuiQuanModel alloc] init];
            
            [youHuiQuanModel yy_modelSetWithDictionary:dataList];
            
            [dataMarray addObject:youHuiQuanModel];
           
        }
        
        self.dataArray = dataMarray;
        
        [_mineTableView reloadData];
       
        
    }];

}

@end
